package code.whmmm.util.simple;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * <br/><br/>
 * 创建时间: 2020/1/8 9:22
 *
 * @author whmmm
 */
public class PathUtil {

    private static String PREFIX = "jar:file:/";

    /**
     * 获取当前 jar 文件的目录
     *
     * @return
     */
    public static String getWorkPath() {

        String url = PathUtil.class.getClassLoader().getResource("").toString();
        url = url.split("/target")[0];

        if (url.startsWith(PREFIX)) {
            System.out.println(url);
            url = url.replace("jar:", "");
            url = url.split(".jar")[0];
            url = url.substring(0, url.lastIndexOf("/"));
        }
        try {
            return URLDecoder.decode(url,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 获取系统当前所在目录
     *
     * @return
     */
    public static String getSystemWorkPath() {
        return System.getProperty("user.dir");
    }

}
